<?php

namespace App\Admin\Controllers\banner;

use App\Common\statement\CommonStatement;
use App\Models\banner\Banner;
use App\Models\banner\BannerCate;
use App\Services\banner\BannerService;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class BannerController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '轮播图';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Banner);
        $grid->model()->orderByDesc('id');
        $grid->disableExport();
        $grid->column('id', __('Id'));
        $grid->column('title', __('Title'));
//        $grid->column('url', __('Url'))->width(50)->display(function () {
//
//            if ($this->type == 1){
//                //跳转外链
//                return "<a target=\"_blank\" href=\"".$this->url."\">".$this->url."</a>";
//            }else{
//                return $this->url;
//            }
//
//        });

        $grid->column('cate.cate_name', __('Cate'));

        $grid->column('img', __('Img'))
            ->lightbox(['zooming' => true,'width' => 50, 'height' => 50, 'class' => ['circle', 'thumbnail']]);

//        $grid->column('type_text', __('跳转类型'));

        $grid->column('status', __('Status'))->switch(CommonStatement::status_switch);

        $grid->column('sort', __('Sort'))->editable();

        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Banner::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('title', __('Title'));
//        $show->field('url', __('Url'));
//        $show->field('cid', __('Cid'));
        $show->field('img', __('Img'));
//        $show->field('type', __('type'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('status', __('Status'));
        $show->field('sort', __('Sort'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Banner);

        $form->text('title', __('Title'));
//        $form->text('url', __('Url'))->help("链接需带有http(s)://前缀");
        $form->select('cid', __('分类'))->options(BannerService::getCate());
        $form->image('img', __('Img'));
//        $form->select('type', __('跳转类型'))
//            ->options(CommonStatement::banner_jump_type)
//            ->default(CommonStatement::banner_default_type);
        $form->switch('status', __('Status'))
            ->states(CommonStatement::status_switch)
            ->default(CommonStatement::default_status);
        $form->number('sort', __('Sort'))->default(99);

        return $form;
    }
}
